package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.service.DeviceService;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvOrdersdetail;
import com.ruoyi.service.IAdvOrdersdetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/ordersdetail")
public class AdvOrdersdetailController extends BaseController {

    @Autowired
    private IAdvOrdersdetailService advOrdersdetailService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvOrdersdetail advOrdersdetail)
    {
        startPage();
        System.out.println("获取到的参数: " + advOrdersdetail);
        List<AdvOrdersdetail> list = advOrdersdetailService.selectAdvOrdersdetailList(advOrdersdetail);
        return getDataTable(list);
    }

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:list')")
    @GetMapping("/list/app/{ordersId}")
    public TableDataInfo listApp(@PathVariable("ordersId") Long ordersId)
    {
        startPage();
        List<AdvOrdersdetail> list = advOrdersdetailService.selectAdvOrdersdetailListApp(ordersId);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvOrdersdetail advOrdersdetail)
    {
        List<AdvOrdersdetail> list = advOrdersdetailService.selectAdvOrdersdetailList(advOrdersdetail);
        ExcelUtil<AdvOrdersdetail> util = new ExcelUtil<AdvOrdersdetail>(AdvOrdersdetail.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:query')")
    @GetMapping(value = "/{advOrdersdetailId}")
    public AjaxResult getInfo(@PathVariable("advOrdersdetailId") Long advOrdersdetailId)
    {
        return success(advOrdersdetailService.selectAdvOrdersdetailByAdvOrdersdetailId(advOrdersdetailId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvOrdersdetail advOrdersdetail)
    {
        return toAjax(advOrdersdetailService.insertAdvOrdersdetail(advOrdersdetail));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvOrdersdetail advOrdersdetail)
    {
        System.out.println("修改------》"+advOrdersdetail);
        return toAjax(advOrdersdetailService.updateAdvOrdersdetail(advOrdersdetail));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:ordersdetail:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advOrdersdetailIds}")
    public AjaxResult remove(@PathVariable Long[] advOrdersdetailIds)
    {
        return toAjax(advOrdersdetailService.deleteAdvOrdersdetailByAdvOrdersdetailIds(advOrdersdetailIds));
    }

    /**
     * 硬件订单明细
     * @param advOrdersId
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('system:ordersdetail:queryOrderByMore')")
    @GetMapping("/queryOrderByMore/{advOrdersId}")
    public TableDataInfo queryOrderByMore(@PathVariable("advOrdersId")Long advOrdersId){
        List<AdvOrdersdetail> advOrdersdetail =  advOrdersdetailService.selectOrderByMore(advOrdersId);
        for (AdvOrdersdetail ordersdetail : advOrdersdetail) {
            // 将ProductPrice从String类型转换为double
            double productPrice = Double.parseDouble(ordersdetail.getProductPrice());
            // 将OrdersNum从Long类型转换为double
            double ordersNum = ordersdetail.getOrdersNum().doubleValue();
            // 计算总价
            double ordersAmount = productPrice * ordersNum;
            // 设置总价
            ordersdetail.setOrdersamount(ordersAmount);
        }
        return getDataTable(advOrdersdetail);
    }

    /**
     * 内容订单明细
     * @param advOrdersId
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('system:ordersdetail:queryOrderByMoreTwo')")
    @GetMapping("/queryOrderByMoreTwo/{advOrdersId}")
    public TableDataInfo queryOrderByMoreTwo(@PathVariable("advOrdersId")Long advOrdersId){
        List<AdvOrdersdetail> advOrdersdetail =  advOrdersdetailService.selectOrderByMoreTwo(advOrdersId);
        for (AdvOrdersdetail ordersdetail : advOrdersdetail) {
            // 将ProductPrice从String类型转换为double
            double productPrice = Double.parseDouble(ordersdetail.getProductPrice());
            // 将OrdersNum从Long类型转换为double
            double ordersNum = ordersdetail.getOrdersNum().doubleValue();
            // 计算总价
            double ordersAmount = productPrice * ordersNum;
            // 设置总价
            ordersdetail.setOrdersamount(ordersAmount);
        }
        return getDataTable(advOrdersdetail);
    }

    @PreAuthorize("@ss.hasAnyPermi('system:ordersdetail:queryOrderByMoreTwo')")
    @GetMapping("/queryOrderByMoreThree/{advOrdersId}")
    public SimpleResult queryOrderByMoreThree(@PathVariable("advOrdersId")Long advOrdersId){
        List<AdvOrdersdetail> advOrdersdetail =  advOrdersdetailService.queryOrderByMoreThree(advOrdersId);
        if(Objects.isNull(advOrdersdetail)) {
            return SimpleResult.error("暂无赠送订单");
        }
        for (AdvOrdersdetail ordersdetail : advOrdersdetail) {
            // 将ProductPrice从String类型转换为double
            double productPrice = Double.parseDouble(ordersdetail.getProductPrice());
            // 将OrdersNum从Long类型转换为double
            double ordersNum = ordersdetail.getOrdersNum().doubleValue();
            // 计算总价
            double ordersAmount = productPrice * ordersNum;
            // 设置总价
            ordersdetail.setOrdersamount(ordersAmount);
        }
        return SimpleResult.success(advOrdersdetail, "获取成功");
    }

    /**
     * 获取已购内容
     */
    @GetMapping("/findBuy/{advMembersId}")
    public TableDataInfo findBuy(@PathVariable Long advMembersId) {
        startPage();
        List<AdvOrdersdetail> list = advOrdersdetailService.findBuy(advMembersId);
        return getDataTable(list);
    }

    /**
     * 同步内容产品
     */
    @PostMapping("/sync/{advMembersId}/{advProductIds}")
    public SimpleResult syncProduct(@PathVariable("advMembersId") String advMembersId,
                                    @PathVariable("advProductIds") Long[] advProductIds) {
        int i = advOrdersdetailService.syncProduct(advMembersId, advProductIds);
        if(i == 0) {
            return SimpleResult.success("已取消所有同步");
        }
        return SimpleResult.success("同步设置完成");
    }

    /**
     * 更新订单数量
     */
    @GetMapping("/productList")
    public TableDataInfo productList(AdvOrdersdetail advOrdersdetail){
        startPage();
        List<AdvOrdersdetail> list = advOrdersdetailService.queryProductList(advOrdersdetail);
        return getDataTable(list);
    }

}
